package com.travel.server.manager.impl.command.login;

import com.alibaba.fastjson.JSONObject;
import com.travel.common.CommonConstant;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.login.LoginRequest;
import com.travel.server.manager.dto.response.login.LoginResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.DepartmentMenuDal;
import com.travel.server.manager.impl.dal.UserDal;
import com.travel.server.manager.model.UserInfo;
import com.travel.server.manager.model.dto.PermissionDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/18 11:13
 * @Desc:
 */
@Component
@Slf4j
public class LoginCommand extends AbstractCommand<LoginRequest, LoginResponse> {

    @Autowired
    private UserDal userDal;

    @Autowired
    private DepartmentMenuDal departmentMenuDal;

    @Override
    protected void checkInput(LoginRequest request) {
        if(StringUtils.isBlank(request.getAccount()) || StringUtils.isBlank(request.getPassword())){
            throw new WorkException(WorkCode.INVALID_PARAM);
        }
    }

    @Override
    public LoginResponse handle(LoginRequest request) {
        checkInput(request);

        //判断验证码是否正确
        if(!validateVerityCode(request.getVerifyCode())){
            throw new WorkException(WorkCode.VERIFY_CODE_ERROR);
        }
        //账号密码验证
        UserInfo userInfo = userDal.select(request.getAccount(), request.getPassword());
        if(userInfo == null){
            log.info("登录账号密码不正确, account {} password {}", request.getAccount(), request.getPassword());
            throw new WorkException(WorkCode.ACCOUNT_ERROR);
        }
        if(!userInfo.getEnable().equals(CommonConstant.YES)){
            throw new WorkException(WorkCode.ACCOUNT_UN_ABLE);
        }
        userInfo.setLastLoginTime(new Date());
        userDal.insertOrUpdate(userInfo);
        //用户信息存入缓存中
        setAttribute(CommonConstant.SESSION_USER_INFO, userInfo);

        LoginResponse response = new LoginResponse();
        response.setPermission(makePermission(userInfo));
        response.setName(userInfo.getName());
        response.setHeadUrl(userInfo.getHeadUrl());
        return response;
    }

    private JSONObject makePermission(UserInfo userInfo){
        JSONObject permission = new JSONObject();
        if(userInfo.getId() != null) {
            List<PermissionDto> permissionList = departmentMenuDal.selectUserPermission(userInfo.getId());
            if(!CollectionUtil.isEmpty(permissionList)){
                for(PermissionDto dto : permissionList){
                    permission.put(dto.getMenuCode() + CommonConstant.EMPTY_STR, CommonConstant.YES);
                }
            }
        }
        return permission;
    }

    private boolean validateVerityCode(String verifyCode){
        HttpServletRequest request = getHttpServletRequest();
        Object codeObj = request.getSession().getAttribute(CommonConstant.LOGIN_VERIFY_CODE);
        if(codeObj == null){
            return true;
        }
        log.info("登录验证码{}", codeObj);
        if(StringUtils.isBlank(verifyCode)){
            return false;
        }
        if(!verifyCode.equalsIgnoreCase(String.valueOf(codeObj))){
            return false;
        }
        return true;
    }
}
